//
//  UUKeeperVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 24/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUKeeperVC.h"
#import "UUBaseTableViewCell.h"
#import "UUPermissionUtility.h"
#import "UUResourceManager.h"
#import "UUKeeperManager.h"
#import "UUPermissionManager.h"
#import "UUKeeperEditVC.h"
#import "UUUserManager.h"

@interface UUKeeperCell : UUBaseTableViewCell

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel *mobileLabel;
@property (nonatomic, weak) IBOutlet UILabel *roleLabel;

@end

@implementation UUKeeperCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.roleLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
}

@end

////////////////////////////////////////////////////////////

@interface UUKeeperVC () <UITableViewDataSource, UITableViewDelegate, UUKeeperManagerObserver>

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray<NSNumber *> *keepers;

@end

@implementation UUKeeperVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.backgroundColor = COMMON_BACKGROUND_COLOR;
    
    if ([GetPermissionManager() checkPermission:UUPermissionFlagKeeperReadWrite]) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:GetResourceManager().addIconText
                                                                                  target:self
                                                                                selector:@selector(addKeeper:)];
    }
    
    [self reloadKeepers];
    
    [GetKeeperManager() addObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)reloadKeepers {
    self.keepers = [GetKeeperManager() fetchKeepers];
    [self.tableView reloadData];
}

- (IBAction)addKeeper:(id)sender {
    UUKeeperEditVC *editVC = [UUKeeperEditVC instanceOfAppropriate];
    editVC.editable = YES;
    [self.navigationController pushViewController:editVC animated:YES];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.keepers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UUKeeperCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    NSNumber *keeperId = self.keepers[indexPath.row];
    UUKeeperModel *keeper = [GetKeeperManager() fetchKeeperWithKeeperId:keeperId];
    
    cell.nameLabel.text = keeper.name;
    cell.mobileLabel.text = keeper.mobile;
    cell.roleLabel.text = [UUPermissionUtility roleNameForRoleId:keeper.roleId];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSNumber *keeperId = self.keepers[indexPath.row];
    
    UUKeeperEditVC *editVC = [UUKeeperEditVC instanceOfAppropriate];
    editVC.editable = NO;
    editVC.keeperId = keeperId;
    [self.navigationController pushViewController:editVC animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *keeperId = self.keepers[indexPath.row];
    return [GetPermissionManager() checkPermission:UUPermissionFlagKeeperReadWrite] && ![[GetUserManager() fetchUserInfo].userId isEqual:keeperId];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [UIAlertController presentAlertControllerWithTitle:@"确定删除该管理员吗？" message:nil doneBlock:^{
            NSNumber *keeperId = self.keepers[indexPath.row];
            
            ShowIndicator();
            WEAK_SELF();
            [GetKeeperManager() asyncDeleteKeeperWithKeeperId:keeperId verifyCode:nil completeBlock:^(NSError *error) {
                HideIndicatorInView(weakSelf.view);
                
                if (error != nil) {
                    ShowTipsOnView(error.localizedDescription, weakSelf.view);
                }
            }];
        } cancelBlock:nil];
    }
}

#pragma mark - UUKeeperManagerObserver methods

- (void)keeperManagerDidUpdateKeepers {
    [self reloadKeepers];
}

@end
